import requests
import json

# 定义API端点和请求参数
url = 'http://localhost/v1/chat-messages'
api_key = 'app-aOWQq2zw4zcYzvtqh6ormCFw'  # 替换为实际的API密钥

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}

payload = {
    'inputs': {},
    'query': '我好难受',
    'response_mode': 'blocking',  # 改为blocking模式获取完整JSON响应
    'conversation_id': '',
    'user': 'abc-123',
    'files': [
        {
            'type': 'image',
            'transfer_method': 'remote_url',
            'url': 'https://cloud.dify.ai/logo/logo-site.png'
        }
    ]
}

# 发送POST请求
try:
    print('正在发送请求到:', url)
    response = requests.post(url, headers=headers, json=payload)
    
    # 检查响应
    print(f'响应状态码: {response.status_code}')
    print(f'响应头: {dict(response.headers)}')
    print(f'响应内容: {response.text[:500]}...' if len(response.text) > 500 else f'响应内容: {response.text}')
    
    if response.status_code == 200:
        try:
            json_response = response.json()
            print('请求成功:', json_response)
        except requests.exceptions.JSONDecodeError:
            print('响应不是有效的JSON格式')
    else:
        print('请求失败:', response.status_code)
        print('错误信息:', response.text)
except requests.exceptions.ConnectionError as e:
    print('连接错误: 无法连接到服务器', str(e))
except requests.exceptions.RequestException as e:
    print('请求异常:', str(e))
except Exception as e:
    print('其他错误:', str(e))
